home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 July: Mac OS SDK / Dev.CD Jul 96 SDK / Dev.CD Jul 96 SDK1.toast / Development Kits (Disc 1) / Apple Game Sprockets / Examples / InputSprocketTest / InputSprocketSimpleTest.cp next >
Encoding:
Text File  |  1996-05-16  |  13.1 KB  |  678 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "InputSprocket.h"
  4. #include "SIOUX.h"
  5.  
  6. enum
  7. {
  8.     kNeedCount = 8
  9. };
  10.  
  11. enum
  12. {
  13.     kIconSuiteID_XThrust = 128, 
  14.     kIconSuiteID_YThrust,
  15.     kIconSuiteID_ZThrust,
  16.     kIconSuiteID_Look,
  17.     kIconSuiteID_Fire,
  18.     kIconSuiteID_Thrust,
  19.     kIconSuiteID_Pause,
  20.     kIconSuiteID_Start
  21. };
  22.  
  23. Boolean gIsSuspended = false;
  24. ISpElementListReference gVirtualList = NULL;
  25. ISpElementReference gVirtualElements[kNeedCount] = {nil, nil, nil, nil, nil, nil, nil, nil};
  26.       
  27.  
  28.  
  29. void ShowFourByte(OSType fourByte);
  30. void ShowFourByte(OSType fourByte)
  31. {
  32.     putchar(((fourByte & 0xff000000) >> 24));
  33.     putchar(((fourByte & 0x00ff0000) >> 16));
  34.     putchar(((fourByte & 0x0000ff00) >> 8));
  35.     putchar(((fourByte & 0x000000ff) >> 00));
  36. }
  37.  
  38. void ShowStr63(const Str63 &theStr);
  39. void ShowStr63(const Str63 &theStr)
  40. {
  41.     int i;
  42.     int len = theStr[0];
  43.     if (len > 63) { len = 63; }
  44.     
  45.     for(i=1; i <= len; i++)
  46.     {
  47.         putchar(theStr[i]);
  48.     }
  49. }
  50.  
  51.  
  52. void PrintEvent(ISpElementEventPtr theEvent);
  53. void PrintEvent(ISpElementEventPtr theEvent)
  54. {
  55.     float temp1 = ((float) theEvent->data)/((float) 0xFFFFFFFFU);
  56.     float temp2 = 2.0*temp1-1.0;
  57.     
  58.     printf("when = [%lu] [%lu]\n", theEvent->when.hi, theEvent->when.lo);
  59.     printf("data = 0x%lx (%f%) (%f%)\n", theEvent->data, temp1, temp2);
  60.     printf("element = 0x%lx\n", theEvent->element);
  61.     printf("refcon = %lx\n", theEvent->refCon);
  62.     
  63.     ISpElementInfo info;
  64.     ISpElement_GetInfo(theEvent->element, &info);
  65.     
  66.     printf("label = ");
  67.     ShowFourByte(info.theLabel);
  68.     printf("\n");
  69.     
  70.     printf("kind = ");
  71.     ShowFourByte(info.theKind);
  72.     printf("\n");
  73.     
  74.     printf("string = ");
  75.     ShowStr63(info.theString);
  76.     printf("\n");
  77.     
  78.     printf("\n\n");
  79. }
  80.  
  81.  
  82. void PrintElementBlock(ISpElementReference *theElementReferences, UInt32 count);
  83. void PrintElementBlock(ISpElementReference *theElementReferences, UInt32 count)
  84. {
  85.     printf("count = %d\n");
  86.  
  87.     int itr;
  88.     
  89.     for(itr = 0; itr < count; itr++)
  90.     {
  91.         printf("    element #%d\n",itr);
  92.         
  93.         ISpElementInfo info;
  94.  
  95.         ISpElement_GetInfo(theElementReferences[itr], &info);
  96.  
  97.         printf("        label = ");
  98.         ShowFourByte(info.theLabel);
  99.         printf("\n");
  100.  
  101.         printf("        kind = ");
  102.         ShowFourByte(info.theKind);
  103.         printf("\n");
  104.  
  105.         printf("        string = ");
  106.         ShowStr63(info.theString);
  107.         printf("\n\n");
  108.     }
  109. }
  110.  
  111. // if whichList is NULL then we use the global list
  112. static void GetAndPrintEvents(ISpElementListReference whichList)
  113. {
  114.     OSErr err;
  115.     
  116.     if (whichList == NULL)
  117.     {
  118.         err = ISpGetGlobalElementList(&whichList);
  119.         if (err)
  120.         {
  121.             printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  122.             return;
  123.         }
  124.     }
  125.  
  126.     err = ISpElementList_Flush(whichList);
  127.     if (err)
  128.     {
  129.         printf("••• ISpElementList_Flush returned error (%ld)\n",err);
  130.         return;
  131.     }
  132.  
  133.     printf("getting events (press command to end)\n");
  134.     
  135.     while(1)
  136.     {
  137.         ISpElementEvent event;
  138.         Boolean wasEvent;
  139.  
  140.         err = ISpElementList_GetNextEvent(whichList, sizeof(event), &event, &wasEvent);
  141.         if (err)
  142.         {
  143.             printf("••• ISpElementList_GetNextEvent returned error (%ld)\n",err);
  144.             return;
  145.         }
  146.  
  147.         if (wasEvent)
  148.         {
  149.             PrintEvent(&event);
  150.         }
  151.         
  152.         KeyMap theKeys;
  153.         GetKeys(theKeys);
  154.  
  155.         if ((theKeys[1] & 0x8000))
  156.         {
  157.             break;
  158.         }
  159.         
  160.         SIOUXHandleOneEvent(nil);
  161.       }
  162. }
  163.  
  164. static void TestListOfDevices(void)
  165. {
  166.     ISpDeviceReference theDevices[100];
  167.     UInt32 deviceCount;
  168.     UInt32 deviceBufferSize = 100;
  169.     UInt32 deviceItr;
  170.     OSErr err;
  171.     
  172.     err = ISpDevices_Extract(deviceBufferSize, &deviceCount, theDevices);
  173.     if (err)
  174.     {
  175.         printf("••• ISpDevices_Extract returned error (%ld)\n",err);
  176.         return;
  177.     }
  178.     
  179.     printf("# of devices = %ld\n",deviceCount);
  180.     for(deviceItr = 0; deviceItr < deviceCount; deviceItr++)
  181.     {
  182.         printf("    device #%ld\n",deviceItr);
  183.         ISpDeviceDefinition theDfn;
  184.         
  185.         err = ISpDevice_GetDefinition(theDevices[deviceItr], sizeof(ISpDeviceDefinition), &theDfn);
  186.         if (err)
  187.         {
  188.             printf("••• ISpDevice_GetDefinition returned error (%ld)\n",err);
  189.             return;
  190.         }
  191.         
  192.         printf("        name = ");
  193.         ShowStr63(theDfn.deviceName);
  194.         printf("\n");
  195.         
  196.         printf("        class = ");
  197.         ShowFourByte(theDfn.theDeviceClass);
  198.         printf("\n");
  199.  
  200.         printf("        device identifier = ");
  201.         ShowFourByte(theDfn.theDeviceIdentifier);
  202.         printf("\n");
  203.  
  204.         printf("        permanent id = %ld\n",theDfn.permanentID);
  205.         
  206.         printf("\n\n");
  207.     }
  208. }
  209.  
  210.  
  211. static void TestListOfElements(void)
  212. {
  213.     ISpElementListReference globalList;
  214.     OSErr err;
  215.     
  216.     err = ISpGetGlobalElementList(&globalList);
  217.     if (err)
  218.     {
  219.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  220.         return;
  221.     }
  222.     
  223.     ISpElementReference theElementReferences[200];
  224.     UInt32 bufferSize = 200;
  225.     UInt32 count;
  226.  
  227.     err = ISpElementList_Extract(globalList, bufferSize, &count, theElementReferences);
  228.     if (err)
  229.     {
  230.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  231.         return;
  232.     }
  233.     
  234.     PrintElementBlock(theElementReferences, count);
  235. }
  236.  
  237.  
  238. static void TestListOfButtons(void)
  239. {
  240.     ISpElementListReference globalList;
  241.     OSErr err;
  242.     
  243.     err = ISpGetGlobalElementList(&globalList);
  244.     if (err)
  245.     {
  246.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  247.         return;
  248.     }
  249.     
  250.     ISpElementReference theElementReferences[200];
  251.     UInt32 bufferSize = 200;
  252.     UInt32 count;
  253.  
  254.     err = ISpElementList_ExtractByKind(globalList, kISpElementKind_Button, bufferSize, &count, theElementReferences);
  255.     if (err)
  256.     {
  257.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  258.         return;
  259.     }
  260.     
  261.     PrintElementBlock(theElementReferences, count);
  262. }
  263.  
  264.  
  265. static void TestListOfDPads(void)
  266. {
  267.     ISpElementListReference globalList;
  268.     OSErr err;
  269.     
  270.     err = ISpGetGlobalElementList(&globalList);
  271.     if (err)
  272.     {
  273.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  274.         return;
  275.     }
  276.     
  277.     ISpElementReference theElementReferences[200];
  278.     UInt32 bufferSize = 200;
  279.     UInt32 count;
  280.  
  281.     err = ISpElementList_ExtractByKind(globalList, kISpElementKind_DPad, bufferSize, &count, theElementReferences);
  282.     if (err)
  283.     {
  284.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  285.         return;
  286.     }
  287.     
  288.     PrintElementBlock(theElementReferences, count);
  289. }
  290.  
  291.  
  292. static void TestListOfAxes(void)
  293. {
  294.     ISpElementListReference globalList;
  295.     OSErr err;
  296.     
  297.     err = ISpGetGlobalElementList(&globalList);
  298.     if (err)
  299.     {
  300.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  301.         return;
  302.     }
  303.     
  304.     ISpElementReference theElementReferences[200];
  305.     UInt32 bufferSize = 200;
  306.     UInt32 count;
  307.  
  308.     err = ISpElementList_ExtractByKind(globalList, kISpElementKind_Axis, bufferSize, &count, theElementReferences);
  309.     if (err)
  310.     {
  311.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  312.         return;
  313.     }
  314.     
  315.     PrintElementBlock(theElementReferences, count);
  316. }
  317.  
  318.  
  319. static void TestListOfXAxes(void)
  320. {
  321.     ISpElementListReference globalList;
  322.     OSErr err;
  323.     
  324.     err = ISpGetGlobalElementList(&globalList);
  325.     if (err)
  326.     {
  327.         printf("••• ISpGetGlobalElementList returned error (%ld)\n",err);
  328.         return;
  329.     }
  330.     
  331.     ISpElementReference theElementReferences[200];
  332.     UInt32 bufferSize = 200;
  333.     UInt32 count;
  334.  
  335.     err = ISpElementList_ExtractByLabel(globalList, kISpElementLabel_XAxis, bufferSize, &count, theElementReferences);
  336.     if (err)
  337.     {
  338.         printf("••• ISpElementList_Extract returned error (%ld)\n",err);
  339.         return;
  340.     }
  341.     
  342.     PrintElementBlock(theElementReferences, count);
  343. }
  344.  
  345.  
  346. static void TestSuspend(void)
  347. {
  348.     OSErr err;
  349.     
  350.     err = ISpSuspend();
  351.     if (err)
  352.     {
  353.         printf("••• ISpSuspend returned error (%ld)\n",err);
  354.         return;
  355.     }
  356.     
  357.     gIsSuspended = true;
  358. }
  359.  
  360.  
  361. static void TestResume(void)
  362. {
  363.     OSErr err;
  364.     
  365.     err = ISpResume();
  366.     if (err)
  367.     {
  368.         printf("••• ISpResume returned error (%ld)\n",err);
  369.         return;
  370.     }
  371.     
  372.     gIsSuspended = false;
  373. }
  374.  
  375.  
  376. static void TestCreateNeeds(void)
  377. {
  378.     OSErr err;
  379.     
  380.     static ISpNeed myNeeds[kNeedCount] =
  381.     {
  382.         { "\pForward Thrust",    kIconSuiteID_YThrust,     kISpElementKind_Axis,        kISpElementLabel_YAxis,    0                            },
  383.         { "\pSide Thrust",         kIconSuiteID_XThrust,    kISpElementKind_Axis,        kISpElementLabel_XAxis,    0                            },
  384.         { "\pVertical Thrust",     kIconSuiteID_ZThrust,     kISpElementKind_Axis,        kISpElementLabel_ZAxis,    0                            },
  385.         { "\pLook",             kIconSuiteID_Look,        kISpElementKind_Movement,    kISpElementLabel_None,    0                            },
  386.         { "\pFire",             kIconSuiteID_Fire,        kISpElementKind_Button,        kISpElementLabel_Fire,    0                            },
  387.         { "\pThrust",             kIconSuiteID_Thrust,     kISpElementKind_Button,        kISpElementLabel_Fire,    0                            },
  388.         { "\pPause",             kIconSuiteID_Pause,        kISpElementKind_Button,        kISpElementLabel_None,    kISpNeedFlag_NoMultiConfig    },
  389.         { "\pStart/Stop",         kIconSuiteID_Start,     kISpElementKind_Button,        kISpElementLabel_Start,    kISpNeedFlag_NoMultiConfig    }
  390.     };
  391.     
  392.     if (gVirtualList == NULL)
  393.     {
  394.         printf("creating virtual elements\n");
  395.         
  396.           err = ISpElement_NewVirtualFromNeeds(kNeedCount, myNeeds, gVirtualElements, 0);
  397.         if (err)
  398.         {
  399.             printf("••• ISpElement_NewVirtualFromNeeds returned error (%ld)\n",err);
  400.             return;
  401.         }
  402.         
  403.         err = ISpElementList_New(
  404.             kNeedCount,                // count
  405.             gVirtualElements,        // needs
  406.             &gVirtualList,            // virtual elements
  407.             0);                        // flags
  408.         if (err)
  409.         {
  410.             printf("••• ISpElementList_New returned error (%ld)\n",err);
  411.             return;
  412.         }
  413.               
  414.         err = ISpInit(kNeedCount,    // count
  415.                 myNeeds,            // needs
  416.                 gVirtualElements,    // virtual elements
  417.                 'ISpT',                // app
  418.                 'foob',                // sub
  419.                 0,                    // flags
  420.                 0,                    // set list resource id
  421.                 0);                    // version
  422.         if (err)
  423.         {
  424.             printf("••• ISpInit returned error (%ld)\n",err);
  425.             return;
  426.         }
  427.     }
  428. }
  429.  
  430.  
  431. static void TestDestroyNeeds(void)
  432. {
  433.     OSErr err;
  434.     
  435.     if (gVirtualList != NULL)
  436.     {
  437.         err = ISpElementList_Dispose(gVirtualList);
  438.         gVirtualList = NULL;
  439.         if (err)
  440.         {
  441.             printf("••• ISpElementList_Dispose returned error (%ld)\n",err);
  442.             return;
  443.         }
  444.         
  445.          err = ISpStop();
  446.         if (err)
  447.         {
  448.             printf("••• ISpStop returned error (%ld)\n",err);
  449.             return;
  450.         }
  451.         
  452.         err = ISpElement_DisposeVirtual(kNeedCount, gVirtualElements);
  453.         if (err)
  454.         {
  455.             printf("••• ISpElement_DisposeVirtual returned error (%ld)\n",err);
  456.             return;
  457.         }
  458.     }
  459. }
  460.  
  461.  
  462. static void TestNeedsEvents(void)
  463. {
  464.     TestCreateNeeds();
  465.     
  466.     if (gVirtualList != NULL)
  467.     {
  468.         GetAndPrintEvents(gVirtualList);
  469.     }
  470. }
  471.  
  472.  
  473. static void TestConfiguration(void)
  474. {
  475.     OSErr err;
  476.     
  477.     TestCreateNeeds();
  478.     
  479.     err = ISpConfigure(nil);
  480.     if (err)
  481.     {
  482.         printf("••• ISpConfigure returned error (%ld)\n",err);
  483.         return;
  484.     }
  485. }
  486.  
  487.  
  488. static void TestEnableMouseAndKeyboard(Boolean enable)
  489. {
  490.     enum {
  491.         kDeviceList_COUNT        = 10
  492.     };
  493.     
  494.     UInt32 count;
  495.     ISpDeviceReference deviceList[kDeviceList_COUNT];
  496.     
  497.     // NOTE: This is not the correct way to handle the list count thing.  We
  498.     // should actually call once with NULL for the device list, malloc a list of
  499.     // that size, and call again.
  500.     
  501.     ISpDevices_ExtractByClass(
  502.             kISpDeviceClass_Mouse,
  503.             kDeviceList_COUNT,
  504.             &count,
  505.             deviceList);
  506.     
  507.     if (count > kDeviceList_COUNT)
  508.     {
  509.         count = kDeviceList_COUNT;
  510.     }
  511.     
  512.     if (enable)
  513.     {
  514.         ISpDevices_Activate(
  515.                 count,
  516.                 deviceList);
  517.     }
  518.     else
  519.     {
  520.         ISpDevices_Deactivate(
  521.                 count,
  522.                 deviceList);
  523.     }
  524.     
  525.     ISpDevices_ExtractByClass(
  526.             kISpDeviceClass_Keyboard,
  527.             kDeviceList_COUNT,
  528.             &count,
  529.             deviceList);
  530.     
  531.     if (count > kDeviceList_COUNT)
  532.     {
  533.         count = kDeviceList_COUNT;
  534.     }
  535.     
  536.     if (enable)
  537.     {
  538.         ISpDevices_Activate(
  539.                 count,
  540.                 deviceList);
  541.     }
  542.     else
  543.     {
  544.         ISpDevices_Deactivate(
  545.                 count,
  546.                 deviceList);
  547.     }
  548. }
  549.  
  550.  
  551. void main(void)
  552. {
  553.     // tell SIOUX to shut up
  554.     SIOUXSettings.autocloseonquit = true;
  555.     SIOUXSettings.asktosaveonclose = false;
  556.     
  557.     printf("starting up...\n");
  558.     
  559.     Boolean theDoneFlag = false;
  560.     
  561.     while( !theDoneFlag )
  562.     {
  563.         UInt32 theChoice;
  564.         
  565.         printf("\n\n\n############################################################\n");
  566.         if (gIsSuspended)
  567.         {
  568.             printf("• SUSPENDED •\n");
  569.         }
  570.         
  571.         if (gVirtualList != NULL)
  572.         {
  573.             printf("• NEEDS ARE ACTIVE •\n");
  574.         }
  575.         
  576.         printf("Please select from the following tests:\n");        
  577.         printf("\t 1. Exit\n");
  578. //        printf("\t 2. Run all tests automatically\n");
  579. //        printf("\t 3. Dump information needed for InputSprocket bug reports\n");
  580.         printf("\t 4. List all devices\n");
  581.         printf("\t 5. List all elements\n");
  582.         printf("\t 6. List all buttons\n");
  583.         printf("\t 7. List all directional pads\n");
  584.         printf("\t 8. List all axes\n");
  585.         printf("\t 9. List all X-axes\n");
  586.         printf("\t10. Get events (stop when Control key pressed)\n");
  587.         printf("\t11. Suspend\n");
  588.         printf("\t12. Resume\n");
  589.         printf("\t13. Create needs\n");
  590.         printf("\t14. Destroy needs\n");
  591.         printf("\t15. Get needs events\n");
  592.         printf("\t16. Configuration dialog\n");
  593.         printf("\t17. Enable mouse and keyboard\n");
  594.         printf("\t18. Disable mouse and keyboard\n");
  595.         
  596.         printf("\n\tSelection: ");
  597.         
  598.         char theString[32];
  599.         gets( theString );
  600.         theChoice = atoi( theString );
  601.         
  602.         printf("\n\n");
  603.         
  604.         switch( theChoice )
  605.         {
  606.             case 1:
  607.                 theDoneFlag = true;
  608.                 break;
  609.                 
  610.             case 4:
  611.                 TestListOfDevices();
  612.                 break;            
  613.             
  614.             case 5:
  615.                 TestListOfElements();
  616.                 break;            
  617.             
  618.             case 6:
  619.                 TestListOfButtons();
  620.                 break;            
  621.             
  622.             case 7:
  623.                 TestListOfDPads();
  624.                 break;            
  625.             
  626.             case 8:
  627.                 TestListOfAxes();
  628.                 break;            
  629.             
  630.             case 9:
  631.                 TestListOfXAxes();
  632.                 break;            
  633.             
  634.             case 10:
  635.                 GetAndPrintEvents(NULL);
  636.                 break;            
  637.             
  638.             case 11:
  639.                 TestSuspend();
  640.                 break;            
  641.             
  642.             case 12:
  643.                 TestResume();
  644.                 break;            
  645.             
  646.             case 13:
  647.                 TestCreateNeeds();
  648.                 break;            
  649.             
  650.             case 14:
  651.                 TestDestroyNeeds();
  652.                 break;            
  653.             
  654.             case 15:
  655.                 TestNeedsEvents();
  656.                 break;            
  657.             
  658.             case 16:
  659.                 TestConfiguration();
  660.                 break;            
  661.             
  662.             case 17:
  663.                 TestEnableMouseAndKeyboard(true);
  664.                 break;            
  665.             
  666.             case 18:
  667.                 TestEnableMouseAndKeyboard(false);
  668.                 break;            
  669.             
  670.             default:
  671.                 printf("\nhuh?\n");
  672.                 break;            
  673.         }
  674.         
  675.         printf("\nThank you, please drive through.\n");
  676.     }
  677. }
  678.